package com.gz.seckill.exception;

import com.gz.seckill.vo.RespBean;
import com.gz.seckill.vo.RespBeanEnum;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    // 处理所有的异常
    @ExceptionHandler(Exception.class)
    public RespBean ExceptionHandler (Exception e) {
        // 如果是全局异常，正常处理
        if (e instanceof GlobalException) {
            GlobalException ex = (GlobalException) e;
            return RespBean.error (ex.getRespBeanEnum());
        } else if (e instanceof BindException) {
            // 如果是绑定异常 ：即pojo类里面属性的校验注解不满足，
            // 需要获取改异常 BindException，进行打印
            BindException ex = (BindException) e;
            RespBean respBean = RespBean.error (RespBeanEnum.BING_ERROR);
            respBean.setMessage("参数校验异常～：" + ex.getBindingResult().getAllErrors ().
                    get(0).getDefaultMessage());
            return respBean;
        }
        return RespBean.error (RespBeanEnum.ERROR);
    }

}
